package com.eric.account.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.eric.account.entity.Account;
import com.eric.account.handler.HandlerException;
import com.eric.account.service.AccountService;
import entity.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author:Eric
 * @date:2021/7/6
 */
@RestController
@RequestMapping("/account")
@Slf4j
@Api(tags = "账户控制器")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping
    @ApiOperation("新增账户")
    public CommonResult insert(@RequestBody Account account) {
        accountService.insert(account);
        return CommonResult.success("success");
    }

    @PutMapping
    @ApiOperation("修改账户")
    public CommonResult update(@RequestBody Account account) {
        accountService.update(account);
        return CommonResult.success("success");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除账户")
    public CommonResult delete(@PathVariable Long id) {
        accountService.delete(id);
        return CommonResult.success("success");
    }

    @PostMapping("/reduce")
    @ApiOperation("扣减账户")
    public CommonResult reduce(@RequestBody Account account){
        accountService.reduce(account);
        return CommonResult.success("success");
    }

    /**
     * 配置限流
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @SentinelResource(value = "account/getOne",
            blockHandler = "getOneHandler",
            blockHandlerClass = {HandlerException.class})
    @ApiOperation("获取用户信息")
    public CommonResult getOne(@PathVariable Long id) {
        Account one = accountService.getOne(id);
        return CommonResult.success(one, "success");
    }

}
